define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'microend/shop/index',
                    add_url: 'microend/shop/add',
                    edit_url: 'microend/shop/edit',
                    del_url: 'microend/shop/del',
                    multi_url: 'microend/shop/multi',
                    pass_url:'microend/shop/pass',
                    refuse_url:'microend/shop/refuse',
                    table: 'shop',
                    ajax_url:{
                        btn_1:{data:{'field':"status"},url:"microend/shop/refuse",fc:function(data){
                                return true;
                            }},
                        btn_2:'microend/shop/edit',
                        btn_3:'microend/shop/pass',
                        btn_4:'microend/paike/edit_user?type=1'
                    },
                    btn_mall_window:{
                        btn_array:["提交","取消"],
                        btn_evn_array_length:2,
                        btn_evn_array: {
                            evn_1:function (index,layero) {
                                var frame = Layer.getChildFrame('html', index);
                                var form = $("form",frame);
                                var success = function (data,ret) {
                                    Layer.msg(data.responseJSON.msg);
                                    table.bootstrapTable('refresh');
                                    Layer.close(index);
                                }
                                Fast.api.form_ajax_submit(form,success);
                            },
                            evn_2:function (index,layero) {
                                Layer.close(index);
                            }
                        },
                        area:['800px','400px']
                    },
                }
            });

            var table = $("#table");

            //在表格内容渲染完成后回调的事件
            table.on('post-body.bs.table', function (e, json) {
                $("tbody tr[data-index]", this).each(function () {
                    // if (parseInt($("td:eq(1)", this).text()) == Config.admin.id) {
                    //     $("input[type=checkbox]", this).prop("disabled", true);
                    // }
                });
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'shop_id',
                sortName: 'shop_id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'username', title: __('商家名称')},
                        {field: 'shop_id', title: __('Shop_id')},
                        {field: 'shop_name', title: __('Shop_name')},
                        {field: 'a.mobile', title: __('Mobile')},
                        {field: 'consignor', title: __('Consignor')},
                        {field: 'area_id', title: __('Area_id'),operate: false},
                        {field: 'wanwan_id', title: __('Wanwan_id')},
                        {field: 'a.shop_type', title: __('Shop_type'),  searchList: {"0":"不限","1":"天猫","2":"淘宝"},
                            //operate:'BETWEEN',
                            formatter:function(value,row,index){
                                var keys = typeof this.searchList === 'object' ? Object.keys(this.searchList) : [];
                                value = value === null ? '未提交' : value.toString();
                                var index = keys.indexOf(value);

                                var display = index > -1 ? this.searchList[value] : null;
                                var icon = typeof this.icon !== 'undefined' ? this.icon : null;
                                if (!display) {
                                    display = __(value.charAt(0).toUpperCase() + value.slice(1));
                                }
                                var html = '<span class="text">' + (icon ? '<i class="' + icon + '"></i> ' : '') + display + '</span>';
                                if (this.operate != false) {
                                    html = '<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', display) + '" data-field="' + this.field + '" data-value="' + value + '">' + html + '</a>';
                                }
                                return html;
                            }},
                        {field: 'a.status', title: __('Status'),  searchList: {"0":"待审核","1":"审核通过","2":"审核失败"},
                            //operate:'BETWEEN',
                            formatter:function(value,row,index){
                                var keys = typeof this.searchList === 'object' ? Object.keys(this.searchList) : [];
                                value = value === null ? '未提交' : value.toString();
                                var index = keys.indexOf(value);
                                var display = index > -1 ? this.searchList[value] : null;
                                var icon = typeof this.icon !== 'undefined' ? this.icon : null;
                                if (!display) {
                                    display = __(value.charAt(0).toUpperCase() + value.slice(1));
                                }
                                var html = '<span class="text">' + (icon ? '<i class="' + icon + '"></i> ' : '') + display + '</span>';
                                if (this.operate != false) {
                                    html = '<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', display) + '" data-field="' + this.field + '" data-value="' + value + '">' + html + '</a>';
                                }
                                return html;
                            }},
                        {field: 'shop_cat', title: __('Shop_cat')},
                        {field: 'shop_url', title: __('Shop_url'), },
                        {field: 'shop_pic', title: __('店铺截图'), formatter: Controller.api.formatter.thumb, operate: false},
                        // {field: 'shop_admin_id', title: __('Shop_admin_id')},
                        {field: 'limit_num', title: __('单数限制')},
                        {field: 'left_num', title: __('剩余单数')},
                        {field: 'gold_bean', title: __('隔日单金豆数量'), operate:'BETWEEN'},
                        {field: 'jing_bean', title: __('精刷单金豆数量'), operate:'BETWEEN'},
                        // {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'delete_time', title: __('Delete_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'operate', title: __('Operate'), table: table,buttons: [
                        //
                        //         {name: 'name2', text: '通过', title: '通过', icon: 'fa fa-flash', classname: 'btn btn-xs btn-success btn-dialog', url: 'microend/shop/pass', success:function(data, ret){}, error:function(){}},
                        //         {name: 'name2', text: '编辑', title: '编辑', icon: 'fa fa-flash', classname: 'btn btn-xs btn-primary btn-dialog', url: 'microend/shop/edit', success:function(data, ret){
                        //
                        //         }, error:function(){}},
                        //         {name: 'name2', text: '驳回', title: '驳回', icon: 'fa fa-flash', classname: 'btn btn-xs btn-danger btn-ajax', url: 'microend/shop/refuse', success:function(data, ret){
                        //             if(ret.code == 1){
                        //                 location.reload();
                        //             }
                        //         }, error:function(){}},
                        //
                        //
                        //     ],  events: Table.api.events.operate, formatter: function (value, row, index) {
                        //         console.log(row);
                        //         $(table).data("operate-del", null);
                        //         $(table).data("operate-edit", null);
                        //         return Table.api.formatter.operate.call(this, value, row, index);
                        //     }}
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter:function(value,row,index) {


                                var table = this.table;
                                // 操作配置
                                var options = table ? table.bootstrapTable('getOptions') : {};
                                // 默认按钮组
                                var buttons = $.extend([], this.buttons || []);

                                //console.log(row);
                                console.log(row.status);
                                if(row.status==0) {
                                    if (options.extend.pass_url !== '') {
                                        buttons.push({
                                            name: 'name2',
                                            icon: 'fa fa-flash',
                                            text: "通过",
                                            title: __('Drag to sort'),
                                            extend: 'data-toggle="tooltip" data-name="btn_3" data-title="信息" ',
                                            classname: 'btn btn-xs btn-primary  btn-small-window',
                                            url: options.extend.pass_url
                                        });
                                    }
                                    if (options.extend.refuse_url !== '') {
                                        buttons.push(
                                            {
                                                name: 'upper',
                                                //icon: 'fa fa-trash',
                                                icon: 'fa fa-flash',
                                                text: "驳回",
                                                title: "驳回",
                                                extend: 'data-toggle="tooltip" data-name="btn_1"',
                                                classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                                //url: options.extend.refuse_url
                                            }
                                        );
                                    }
                                }
                                if(row.status==1) {
                                    if (options.extend.edit_url !== '') {
                                        buttons.push({
                                            name: 'edit',
                                            icon: 'fa fa-flash',
                                            text: "编辑",
                                            title: __('Edit'),
                                            extend: 'data-toggle="tooltip" data-name="btn_2" data-title="信息" ',
                                            classname: 'btn btn-xs btn-success btn-small-window',
                                            url: options.extend.edit_url
                                        });
                                    }
                                }


                                return Table.api.buttonlink(this, buttons, value, row, index, 'operate');

                            }
                        }
                    ]
                ]
            });



            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        pass: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                thumb: function (value, row, index) {
                    //if (row.mimetype.indexOf("image") > -1) {
                    var style = row.storage == 'upyun' ? '!/fwfh/120x90' : '';
                    return '<a href="' + row.fullurl + '" target="_blank"><img src="' + row.fullurl + style + '" alt="" style="max-height:90px;max-width:120px"></a>';
                    // } else {
                    //     return '<a href="' + row.fullurl + '" target="_blank">' + __('None') + '</a>';
                    // }
                },
                url: function (value, row, index) {
                    return '<a href="' + row.fullurl + '" target="_blank" class="label bg-green">' + value + '</a>';
                },
            }
        }
    };
    return Controller;
});